package com.shxc.smarttrade.old.modal;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.shxc.smarttrade.old.bean.AccountInfomation;
import com.shxc.smarttrade.old.bean.UserInfo;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;

import org.xchris.x;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/6/8.
 */

public class AccountInfoModal implements IAccount {


    /**
     * 获取账户详情
     *
     * @param userid
     * @param listener
     */
    @Override
    public void getAccountInfomation(final String accessToken, final int userid, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", userid + "");
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.ACCOUNT_FRAGMENT_ACCOUNT_DETAILS, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
//                Log.e("account", "  userId:" + userid + "  accessToken:" + accessToken + "  resopnse: " + response);
//                Log.e("response11", response);

                if (response != null) {
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = "";
                    String errMsg = "";
                    if (!jsonObject.get("err_code").isJsonNull())
                        errCode = jsonObject.get("err_code").getAsString();
                    if (!jsonObject.get("err_msg").isJsonNull())
                        errMsg = jsonObject.get("err_msg").getAsString();
                    if (errCode.equals("0")) {
                        Gson gson = new Gson();
                        AccountInfomation accountInfomation = gson.fromJson(response, AccountInfomation.class);
                        listener.success(accountInfomation.getData());
                    } else if (errCode.equals("-2")) {
                        listener.faild(errMsg);
                    } else {
                        listener.faild("数据异常");
                    }
                } else {
                    listener.faild("数据异常");
                }
            }
        });
    }

    /**
     * 获取用户信息
     *
     * @param userId
     * @param listener
     */
    @Override
    public void getUserInfo(final String accessToken, final int userId, final IRequestServiceListener listener) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", accessToken);
        map.put("userId", userId + "");
        MyOkHttpUtils.getServiceDatas(x.app(),map, URLContents.ACCOUNT_FRAGMENT_ACCOUNT_USERINFO, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                listener.faild("网络异常");
            }

            @Override
            public void success(String response, int id) {
//                Log.e("response22", response);

                if (response != null) {
                    JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
                    String errCode = "";
                    String errMsg = "";
                    if (!jsonObject.get("err_code").isJsonNull())
                        errCode = jsonObject.get("err_code").getAsString();
                    if (!jsonObject.get("err_msg").isJsonNull())
                        errMsg = jsonObject.get("err_msg").getAsString();
                    if (errCode.equals("0")) {
                        if (!jsonObject.get("data").isJsonNull()) {
                            Gson gson = new Gson();
                            UserInfo userInfo = gson.fromJson(jsonObject.get("data").getAsJsonObject(), UserInfo.class);
                            listener.success(userInfo);
                        } else {
                            listener.faild("数据异常");
                        }
                    } else {
                        if (errCode.equals("-2")) {
                            listener.faild("未开户");
                        } else
                            listener.faild(errMsg);
                    }

                } else {
                    listener.faild("数据异常");
                }
            }
        });
    }
}
